package com.crazymakercircle.node;

import com.crazymakercircle.entity.TaskData;
import com.crazymakercircle.entity.TestResultData;
import com.crazymakercircle.entity.WorkerData;
import com.crazymakercircle.util.JsonUtil;
import com.crazymakercircle.zk.CuratorZKclient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;

/**
 * IM 节点的ZK协调客户端
 * create by 尼恩 @ 疯狂创客圈
 **/
@Data
@Slf4j
public class TestResultNode
{

    //Zk curator 客户端
    private CuratorFramework client = null;

    //保存当前Znode节点的路径，创建后返回
    private String pathRegistered = null;

    private TestResultData testResultData = null;

    private WorkerData workerData = null;

    private TaskData taskData = null;

    private static TestResultNode singleInstance = null;
    private boolean inited = false;


    // 在zookeeper中创建临时节点
    public synchronized void createNode()
    {

        if (inited)
        {
            log.info("节点已经创建");
            return;
        }
        inited = true;
        if (null == client)
        {
            this.client = CuratorZKclient.instance.getClient();
        }
        if (null == testResultData)
        {
            testResultData = new TestResultData();
        }

        if (null == workerData)
        {
            throw new RuntimeException("工作节点数据为空");
        }

        if (null == taskData)
        {
            throw new RuntimeException("任务节点数据为空");
        }

        boolean isTaskExist = CuratorZKclient.instance.isNodeExist(taskData.getRunPath());

        if (!isTaskExist)
        {
            throw new RuntimeException("任务未创建");
        }

        // 创建一个 ZNode 节点
        // 节点的 payload 为当前worker 实例
        testResultData.setName("worker-" + workerData.getId());
        testResultData.setPath(taskData.getRunPath() + "/" + testResultData.getName());

        boolean isResultExist = CuratorZKclient.instance.isNodeExist(testResultData.getPath());

        if (isResultExist)
        {
            log.info("任务已经执行过");
        } else
        {
            log.info("创建执行任务的节点");

            try
            {
                byte[] payload = JsonUtil.object2JsonBytes(testResultData);

                pathRegistered = client.create()
                        .creatingParentsIfNeeded()
                        .withMode(CreateMode.EPHEMERAL)
                        .forPath(testResultData.getPath(), payload);

                log.info("本地节点, path={}, name={}",
                        pathRegistered, testResultData.getName());
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    // 在zookeeper中更新临时节点
    public synchronized void updateNode()
    {

        boolean isResultExist = CuratorZKclient.instance.isNodeExist(testResultData.getPath());

        if (isResultExist)
        {
            byte[] payload = JsonUtil.object2JsonBytes(testResultData);
            CuratorZKclient.instance.updateData(testResultData.getPath(), payload);

        } else
        {
            log.info("任务没有启动");
        }
    }
}